<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/5/31
  Time: 11:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/common/common.jsp" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<script>

    var setting = {
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            enable: true,
            autoCheckTrigger: true
        },
        async: {
            enable: true,
            dataType: "json",
            url: "<%=path%>/menu/ztreeInfo",
            autoParam: ["id", "name"]

        }, view: {
            addHoverDom: function (treeId, treeNode) {
                var aObj = $("#" + treeNode.tId + "_a");
                if (treeNode.editNameFlag || $("#btnGroup" + treeNode.tId).length > 0) return;
                var s = '<span id="btnGroup' + treeNode.tId + '">';
                if (treeNode.level == 0) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >添加</a>';
                } else if (treeNode.level == 1) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">修改</a>';
                    if (treeNode.children == undefined) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#" >删除</a>';
                    }
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >添加</a>';
                } else if (treeNode.level == 2) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">修改</a>';
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#">删除</a>';
                }
                s += '</span>';
                aObj.after(s);
            },
            removeHoverDom: function (treeId, treeNode) {
                $("#btnGroup" + treeNode.tId).remove();
            }
        }
    };

    function addNode(id) {
        alert(id);
        BootstrapDialog.show({
            title: '增加',
            message: $('<div></div>').load('<%=path%>/menu/addInfo'),
            closeable: true,
            buttons: [{
                label: '取消',
                action: function (dialog) {
                    dialog.close();
                }

            }, {
                label: '确定',
                action: function (dialog) {
                    $.ajax({
                        url: "<%=path%>/menu/insertinto",
                        type: "post",
                        dataType: "json",
                        data: {"text": $("#text").val(), "url": $("#url").val(), pid: id},
                        success: function (data) {
                            // $("#table").bootstrapTable("refresh");
                            toastr.success("增加成功");
                            dialog.close();
                            $.fn.zTree.init($("#treeDemo"), setting);

                        },
                        error: function (data) {
                            console.log(data);
                            alert("网络异常");
                        }

                    })

                }
            }
            ]

        })
    }

    function editNode(id) {
        BootstrapDialog.show({
            title: '修改',
            message: $('<div></div>').load('<%=path%>/menu/toUpdate/' + id),
            closeable: true,
            buttons: [{
                label: '取消',
                action: function (dialog) {
                    dialog.close();
                }

            }, {
                label: '确定',
                action: function (dialog) {
                    $.ajax({
                        url: "<%=path%>/menu/updateInfo",
                        type: "get",
                        dataType: "json",
                        // data:$("#uodate_form1").serialize(),
                        data: $("#update_form").serialize(),
                        success: function (data) {
                            // $("#table").bootstrapTable("refresh");
                            toastr.success("修改成功");
                            dialog.close();
                            $.fn.zTree.init($("#treeDemo"), setting);

                        },
                        error: function (data) {
                            console.log(data);
                            alert("网络异常");
                        }

                    })

                }
            }
            ]

        })

    }

    function deleteNode(id) {
        if (confirm("确定删除吗")) {
            $.ajax({
                url: "<%=path%>/menu/deleteInfo/" + id,
                type: "get",
                dataType: "json",
                // data:$("#uodate_form1").serialize(),
                // data:{id:$("#id").val()},
                success: function (data) {

                    toastr.success("删除");
                    $.fn.zTree.init($("#treeDemo"), setting);
                },
                error: function (data) {
                    console.log(data);
                    alert("网络异常");
                }

            })

        }
        ;

    }

    $(document).ready(function () {
        $.fn.zTree.init($("#treeDemo"), setting);
    });
</script>

</body>
<div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>
</div>
</html>

